package mas.experiment_2_wordbook.database;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

import mas.experiment_2_wordbook.diary.Diary;


public class Dao extends Application {
    private DataBaseHelper dbhelper;
    private SQLiteDatabase db;

    public Dao(Context context) {

        dbhelper = new DataBaseHelper(context.getApplicationContext());// 初始化DataBaseHelper对象
    }

    /**
     * 插入
     * @param diary
     */
    public long insert(Diary diary){
        db=dbhelper.getWritableDatabase();
        String str="insert into diary (title,content,date,author,image) values ('"+diary.getTitle()+"','"+
                diary.getContent()+"','"+diary.getDate()+"','"+diary.getAuthor()+"','"+diary.getImage()+"')";

        ContentValues cv = new ContentValues();
        cv.put("title",diary.getTitle());
        cv.put("content",diary.getContent());
        cv.put("date",diary.getDate());
        cv.put("author",diary.getAuthor());
        cv.put("image",diary.getImage());
       return db.insert("diary",null,cv);
        //db.execSQL(str);
       // db.close();
    }

    /**
     * 更新
     * @param d
     */
    public void update(Diary d) {
        db = dbhelper.getWritableDatabase();
        String sql = "update diary set title=?,content=?,image=? where date=? and author=?";
        Object bindArgs[] = new Object[] { d.getTitle(), d.getContent(),d.getImage(),d.getDate(),d.getAuthor() };
        db.execSQL(sql, bindArgs);
        db.close();
    }

    /**
     * 查找
     * @param
     * @return
     */
    @SuppressLint("Range")
    public ArrayList<Diary> find(String str) {
        ArrayList<Diary> diarylist = new ArrayList<>();
        db = dbhelper.getWritableDatabase();
        String sql = "select * from diary where author=?";
        String[] selectionArgs = new String[] { str };
        Cursor cursor = db.rawQuery(sql, selectionArgs);
        // 游标从头读到尾
        while (cursor.moveToNext()) {
                Diary diary = new Diary();
                diary.setAuthor(cursor.getString(cursor.getColumnIndex("author")));
                diary.setContent(cursor.getString(cursor.getColumnIndex("content")));
                diary.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                diary.setDate(cursor.getString(cursor.getColumnIndex("date")));
                diary.setImage(cursor.getString(cursor.getColumnIndex("image")));
                diarylist.add(diary);
        }
        cursor.close();
        db.close();
        return diarylist;
    }

    /**
     * 删除
     * @param d
     */
    public void delete(Diary d) {
        db = dbhelper.getWritableDatabase();
        String sql = "delete from diary where author=? and date=?";
        Object bindArgs[] = new Object[] { d.getAuthor(),d.getDate()};
        db.execSQL(sql, bindArgs);
        db.close();
    }

    /**
     * 退出
     */
//    public void close(){
//        db = dbhelper.getWritableDatabase();
//        if(db != null){
//            db.close();
//        }
//    }
}
